-- Lua 脚本：预占库存加N,可售库存减N
-- ARGV[1] 为预占的数量

-- 获取当前库存信息
local sellableQuantityStr = redis.call('HGET', KEYS[1],"sellableQuantity")
if not sellableQuantityStr then
    error("params is error")
end
local sellableQuantity = tonumber(sellableQuantityStr)
if not sellableQuantity then
    error("withholdingQuantity is not a valid number")
end
-- 检查预占库存是否足够
if  sellableQuantity - ARGV[1] < 0  then
    return false
end

-- 更新预占库存与可售库存
redis.call('HINCRBY', KEYS[1], 'sellableQuantity',  - ARGV[1])
redis.call('HINCRBY', KEYS[1], 'withholdingQuantity', ARGV[1])

return true
